package de.bomzhi.mz.pflection.model.scenario;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

import org.jbehave.scenario.annotations.Given;
import org.jbehave.scenario.annotations.Then;
import org.jbehave.scenario.annotations.When;
import org.jbehave.scenario.steps.Steps;

import com.powerflasher.fdt.core.dom.IDeclaration;
import com.powerflasher.fdt.core.dom.IFunctionDeclaration;
import com.powerflasher.fdt.core.dom.IRoot;
import com.powerflasher.fdt.core.parser.CharStreamFactory;
import com.powerflasher.fdt.internal.core.parser.AS3Parser;
import com.powerflasher.fdt.internal.core.parser.CharStream;
import com.powerflasher.fdt.internal.core.parser.ParseException;

import de.bomzhi.mz.pflection.model.PflectionValuesComputer;
import de.bomzhi.mz.pflection.model.PflectionValuesComputer.FunctionAnalyser;

public class FunctionAnalyserSteps extends Steps {

	private IFunctionDeclaration funcDecl;
	private FunctionAnalyser analyser;

	@SuppressWarnings("restriction")
	@Given("a function:$function")
	public void parseFunction(String function) throws ParseException{
		CharStream stream = CharStreamFactory.createFromString("package {"+function+"};");
		AS3Parser parser = new AS3Parser(stream);
		IRoot root = parser.Root();
		assertThat(root, is(notNullValue()));
		IDeclaration masterDeclaration = root.getMasterDeclaration();
		assertThat(masterDeclaration, is(IFunctionDeclaration.class));
		funcDecl = (IFunctionDeclaration)masterDeclaration;
	}
	
	@When("applying Function Analyser")
	public void applyTheVisitor() {
		analyser = new PflectionValuesComputer.FunctionAnalyser();
		funcDecl.accept(analyser);
	}
	
	@Then("number of blocks is '$blocks'")
	public void checkBlocks(int blocks) {
		assertThat(analyser.getNumberOfBlocks(), is(blocks));
	}

	@Then("number of flow terminations is '$terminations'")
	public void checkTerminations(int terminations) {
		assertThat(analyser.getNumberOfFlowTerminations(), is(terminations));
	}

	@Then("number of conditions is '$conditions'")
	public void checkConditions(int conditions) {
		assertThat(analyser.getNumberOfConditions(), is(conditions));
	}
}
